﻿using System;
using System.Collections.Generic;
using System.Drawing;

namespace PlantsZombies.Common.Bullet
{
    public unsafe abstract class BulletObject
    {
        protected readonly ImageObject[] _imagesBullet;
        private Size _bulletSize;
        protected readonly GameObject _gameObject;
        public BulletObject(Size bulletSize, ImageObject[] images, GameObject gameObject)
        {
            _bulletSize = bulletSize;
            _imagesBullet = images;
            _gameObject = gameObject;
        }
        protected List<BulletPoint> _bullets = new List<BulletPoint>();
        public List<BulletPoint> BulletObjects
        {
            get { return _bullets; }
        }
        public Size BulletSize { get { return _bulletSize; } set { _bulletSize = value; } }
        public virtual void Move(ListGameObject* ptr)
        {
        }
        public virtual void Play(IntPtr hdc)
        {
            for (int i = 0; i < _bullets.Count; i++)
            {
                var item = _bullets[i];
                var img = _imagesBullet[item.Index];
                GDI32.DrawImageTransparenc(hdc, img.BitmapPtr, item.X, item.Y, img.Width, img.Height, BulletSize.Width, BulletSize.Height);
                if (++item.Index >= _imagesBullet.Length)
                    item.Index = 0;
            }
        }
        public virtual void Attack(ListGameObject* ptr)
        {
            CheckAttack(ptr);
        }
        protected virtual void CheckAttack(ListGameObject* ptr)
        {
            for (int i = _bullets.Count - 1; i >= 0; i--)
            {
                var item = _bullets[i];
                var bulletRect = new Rectangle(item.X, item.Y, BulletSize.Width, BulletSize.Height);
                for (int j = _gameObject.Column + 1; j < _gameObject.UseMaxColumn; j++)
                {
                    var p = ptr + _gameObject.Row * _gameObject.MaxColumn + j;
                    if (p->Game.Count == 0) continue;

                    for (int k = 0; k < p->Game.Count; k++)
                    {
                        var game = p->Game[k];
                        if (game == null) continue;
                        if (!game.IsZombie()) continue;
                        var rect = new Rectangle(game.Location.X, game.Location.Y, game.Size.Width, game.Size.Height);
                        if (bulletRect.IntersectsWith(rect))
                        {
                            _bullets.Remove(item);
                            if ((game.Life -= _gameObject.Damage) <= 0)
                                p->Game.RemoveAt(k);
                        }
                    }
                }
            }
        }
        public virtual void UpdateState(float zoomX, float zoomY)
        {
            this.BulletSize = new Size((int)(this.BulletSize.Width * zoomX), (int)(this.BulletSize.Height * zoomY));
            for (int i = 0; i < _bullets.Count; i++)
            {
                var item = _bullets[i];
                item.X = (int)(item.X * zoomX);
                item.Y = (int)(item.Y * zoomY);
            }
        }
    }
}
